// 公用数据持久化
import {getStore, setStore, removeStore} from '@/util/store'
import {logout} from '@/page/api/user'

const common = {
	state: {
		isCollapse:false,
		isFullscreen:false,
		isLock:getStore({
			name:'isLock'
		}) || false,
		lockPass:getStore({
			name:'lockPass'
		}) || '',
		theme:getStore({
			name:'theme'
		}) || '#409EFF',
		themeName:getStore({
			name:'themeName'
		}) || 'themeName',

	},
	actions: {
		LogOut({commit, state}){
			return new Promise((resolve, reject)=>{
				logout().then(()=>{
					commit('CLEAR_LOCK');
				})
			})
		}
	},
	mutations: {
		setCollapse:(state,action) => {
			state.isCollapse = !state.isCollapse;
		},
		setFullScreen:(state,action) => {
			state.isFullscreen = !state.isFullscreen;
		},
		setLock:(state,action) => {
			state.isLock = true;
			setStore({
				name:'isLock',
				type:'session',
				content:state.isLock
			})
		},
		setLockPass:(state,pass) => {
			state.lockPass = pass;
			setStore({
				name:'lockPass',
				content:state.lockPass,
				type:'session'
			})
		},
		removeLock:(state,action) => {
			state.isLock = false;
		},
		setTheme:(state,color) => {
			state.theme = color;
			setStore({
				name:'theme',
				content:state.theme
			})
		},
		setThemeName:(state,themeName) => {
			state.themeName = themeName;
			setStore({
				name:'themeName',
				content:state.themeName
			})
		},
		CLEAR_LOCK:(state)=>{
			state.isLock = false;
			console.log(state.isLock,'===')
			state.lockPass = '';
			removeStore({
                name: 'lockPass'
            });
            removeStore({
                name: 'isLock'
            });
		}
	}
}
export default common